package com.example.opencvtest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.util.EncodingUtils;
import org.opencv.android.Utils;
import org.opencv.core.Mat;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.hardware.Camera.Size;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class OverlayView extends View
{
	private static final String	TAG	= "OverlayView";

	public OverlayView(Context context)
	{
		super(context);
		Log.i(TAG, "OverlayView constructed");
		mBitmap = Bitmap.createBitmap(640, 360, Bitmap.Config.ARGB_8888);
		// Canvas bitmapCanvas = new Canvas(mBitmap);
	}

	public void setImage(Mat image)
	{
		mBitmap = Bitmap.createBitmap(image.width(), image.height(), Bitmap.Config.ARGB_8888);
		// Log.d(TAG, "setImage");
		// create a bitmap from the OpenCV mat
		Utils.matToBitmap(image, mBitmap);

		// redraw the View
		invalidate();
	}

	public void saveCurrentBitmap(String path)
	{
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(path);
			mBitmap.compress(CompressFormat.JPEG, 100, fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
			}
		}
	}

	Bitmap	mBitmap;

	@Override
	protected void onDraw(Canvas canvas)
	{
		// Log.d(TAG, "onDraw");
		if (mBitmap != null)
			canvas.drawBitmap(mBitmap, 0f, 0f, null);
		super.onDraw(canvas);
	}
}
